package com.mxnet.chongjin.supoman.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.android.tu.loadingdialog.LoadingDailog;
import com.mxnet.chongjin.supoman.MainActivity;
import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.adapter.LvConfigGuideAdapter;
import com.mxnet.chongjin.supoman.bean.ConfigGuide;
import com.mxnet.chongjin.supoman.utils.UDPUtils;

import java.util.ArrayList;
import java.util.List;

public class ConfigGuideActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private LinearLayout mLinearLayout;
    private ListView mListView;
    private LvConfigGuideAdapter mLvConfigGuideAdapter;
    private List<ConfigGuide> mList;
    private Button mButton;
    private SharedPreferences mPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config_guide);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = ConfigGuideActivity.this;
        mPreferences = getSharedPreferences("supoman", MODE_PRIVATE);

        initData();
        initView();
    }

    public void initData() {
        mList = new ArrayList();
        ConfigGuide configGuide1 = new ConfigGuide(R.mipmap.configuration_pic_1, getResources().getString(R.string.config_guide_1));
        ConfigGuide configGuide2 = new ConfigGuide(R.mipmap.configuration_pic_2, getResources().getString(R.string.config_guide_2));
        mList.add(configGuide1);
        mList.add(configGuide2);
    }

    public void initView() {
        mListView = findViewById(R.id.lv_config_guide);
        mLvConfigGuideAdapter = new LvConfigGuideAdapter(mContext, mList);
        mListView.setAdapter(mLvConfigGuideAdapter);

        mLinearLayout = findViewById(R.id.ll_config_guide);
        mLinearLayout.setOnClickListener(this);

        mButton = findViewById(R.id.btn_config_guide);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_config_guide:
                Intent intent1 = new Intent(mContext, MainActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.btn_config_guide:
                try {
                    LoadingDailog.Builder loadBuilder = new LoadingDailog.Builder(mContext)
                            .setMessage(getResources().getString(R.string.configuring))
                            .setCancelable(true)
                            .setCancelOutside(true);
                    final LoadingDailog dialog = loadBuilder.create();
                    dialog.setCancelable(false);   //是否能取消弹窗
                    dialog.show();
                    new Thread() {
                        @Override
                        public void run() {
                            UDPUtils.receiveUDP(mContext);
                        }
                    }.start();
                    CountDownTimer downTimer = new CountDownTimer(5 * 1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            new Thread() {
                                @Override
                                public void run() {
                                    UDPUtils.sendUDPBroadcast();
                                }
                            }.start();
                        }

                        @Override
                        public void onFinish() {
                            dialog.dismiss();
                            String MAC = mPreferences.getString("MAC", "");
                            if (MAC != null && MAC.length() > 0) {  //true ||

                                mPreferences.edit().putInt("sequenceNum", 1).commit();
                                Intent intent = new Intent();
                                intent.putExtra("matched", true);
                                intent.setClass(mContext, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }
                            else
                            {
                                Toast.makeText(mContext,"配置失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                    };
                    downTimer.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
